package com.ltmonitor.web.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.aspectj.weaver.ast.Var;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.ltmonitor.entity.DriverInfo;
import com.ltmonitor.service.IDriverInfoService;
import com.ltmonitor.web.util.JsonMessage;

import net.sf.json.JSONObject;

@Controller
@RequestMapping("/driverInfo")
public class DriverInfoAction extends PaginateAction{
	@Autowired
	private IDriverInfoService driverInfoService;


	@RequestMapping("/viewList.action")
	public String list()
	{
		return "/driverInfo/driverInfoList";
	}
	

	@RequestMapping("/view.action")
	public ModelAndView view(Model model,
			@RequestParam(value = "entityId", defaultValue="0") long id) {

		ModelAndView m = new ModelAndView();
		m.setViewName("/driverInfo/editDriverInfo");
		try {
			if (id >0) {
				DriverInfo t = driverInfoService.load(id);
				if(t == null)
				{

					m.addObject("entity", new DriverInfo());		
				}else
				m.addObject("entity", t);
			}else
			{
				m.addObject("entity", new DriverInfo());					
			}

		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
			m.addObject("发生错误:"+ex.getMessage());
		}

		return m;
	}
	/**
	 * 新增驾驶员后的保存操作
	 * 
	 * @return
	 */
	@RequestMapping("/save")
	public String save(DriverInfo d, Model m) throws Exception {

		try {

			d.setCompanyId(this.getOnlineUser().getCompanyId());
			//d.setMainDriver(mainDriver);
			//一个车只能有一个主驾驶，需要将同一车的其他主驾驶设置为false
			this.driverInfoService.saveDriver(d);

			m.addAttribute("entity", d);
			m.addAttribute("message", "保存成功");
		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
			m.addAttribute("message", ex.getMessage());
		}

		m.addAttribute("entity", d);
		return "/driverInfo/editDriverInfo";
	}
	
	/**
	 * 假删除
	 * @param entityId
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/delete.action")
	public JsonMessage delete(long entityId) {
		try {
			this.driverInfoService.deleteById(entityId);
			return jsonSuccess("记录已被成功刪除!");

		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
			return jsonFail(ex.getMessage());
		}

	}
	
	
	/**
	 * 根据车辆id，获取最后一次插卡记录
	 * @param vehicleId
	 * @return 
	 */
	@RequestMapping("/getLastInCardDriver.action")
	public @ResponseBody JsonMessage getLastInCardDriver(@RequestBody String jsonStr) {
		try {
			Map<String, Object> map = JSONObject.fromObject(jsonStr);
			String queryId = "driverInfo.selectLastInCardDriver";
			List result = queryService.query(queryId, map);
			return (new JsonMessage(true, result));
		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
			return jsonFail(ex.getMessage());
		}
	}

}
